#include "input.h"

#include "gl\glfw.h"
#include <memory.h>


// a quite like declaring externs
int Input::keysHit[GLFW_KEY_LAST];
int Input::keysDown[GLFW_KEY_LAST];
MouseInfo Input::Mouse;

int Input::mouseLeftHits;
int Input::mouseRightHits;
int Input::mouseMiddleHits;


bool Input::Init()
{
	memset(keysDown, 0, sizeof(keysDown));
	memset(keysHit, 0, sizeof(keysHit));
	
	return true;
}


// \breif Number of times key was hit since last check
int Input::keyHit( int key )
{
	int timesHit = keysHit[key];
	keysHit[key] = 0;
	return timesHit;
}

int Input::keyDown( int key )
{
	return keysDown[key];
}

int Input::mouseX()
{
	return Mouse.Mx;
}
int Input::mouseY()
{
	return Mouse.My;
}


void Input::MouseButtonCallback( int button, int action )
{
	if (button == GLFW_MOUSE_BUTTON_LEFT)
	{
		Mouse.Mleft = (action == GLFW_PRESS);

		if(action == GLFW_PRESS){		
			mouseLeftHits++;
		}
		
	}
	else if (button == GLFW_MOUSE_BUTTON_RIGHT)
	{
		Mouse.Mright = (action == GLFW_PRESS);

		if(action == GLFW_PRESS){
			mouseRightHits++;
		}

	}
	else if (button == GLFW_MOUSE_BUTTON_MIDDLE)
	{
		Mouse.Mmiddle = (action == GLFW_PRESS);

		if(action == GLFW_PRESS){
			mouseMiddleHits++;
		}    
	}
}

void Input::MousePosCallback( int x, int y )
{
	Mouse.Mx = x;
	Mouse.My = y;
}

void Input::KeyCallback( int key, int action )
{
	if( action == GLFW_PRESS )
	{
		keysDown[key] = 1;
		keysHit[key]++;
	}else{
		keysDown[key] = 0;
	}
}

void Input::CharCallback( int key, int action )
{
}

bool Input::MouseDown( int button )
{
	switch(button)
	{
		case GLFW_MOUSE_BUTTON_LEFT: return Mouse.Mleft; break;
		case GLFW_MOUSE_BUTTON_RIGHT: return Mouse.Mright; break;
		case GLFW_MOUSE_BUTTON_MIDDLE: return Mouse.Mmiddle; break;
	}
	return false;
	
}

int Input::MouseHit( int button )
{
	if(button == GLFW_MOUSE_BUTTON_LEFT)
	{
		int num = mouseLeftHits; 
		mouseLeftHits = 0; 
		return num;
	}else if(button == GLFW_MOUSE_BUTTON_RIGHT)
	{
		int num = mouseRightHits; 
		mouseRightHits = 0; 
		return num; 
	}else if( button == GLFW_MOUSE_BUTTON_MIDDLE)
	{
		int num = mouseMiddleHits;
		mouseMiddleHits = 0; 
		return num; 
	}
	return 0;

}

